<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Models\Product;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\Log;

class ProductsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Product);

        $grid->id('ID')->sortable();
        $grid->column('title', '商品名称')->editable();
        $grid->column('image', "封面")->image('', 50, 50);

        $categories = Category::query()->where('pid', '>', 0)->select('id', 'title')->pluck('title', 'id')->toArray();
        $categories = ['0' => '无分类'] + $categories;

        $grid->column('category_id', '分类')->select($categories);
        $grid->column('url', '跳转链接')->editable();

//        $grid->column('category.title', "分类");
//        $grid->on_sale('已上架')->display(function ($value) {
//            return $value ? '是' : '否';
//        });

        $grid->column('on_sale', '已上架')->switch(
            [
                'on' => ['value' => 1, 'text' => '是'],
                'off' => ['value' => 0, 'text' => '否'],
            ]
        );
        $grid->price('价格');
//        $grid->rating('评分');
//        $grid->sold_count('销量');
//        $grid->review_count('评论数');

        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function ($tools) {
            // 禁用批量删除按钮
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->filter(function ($filter) use ($categories) {
            $filter->disableIdFilter();
            $filter->equal('category_id', '分类')->select($categories);
            $filter->like('title', '商品名称');
            $filter->equal('on_sale', '是否上架')->select(['1' => '是', '0' => '否']);
        });


        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Product);

        // 创建一个输入框，第一个参数 title 是模型的字段名，第二个参数是该字段描述
        $form->text('title', '商品名称')->rules('required');
        $form->select('category_id', '分类')->options(Category::getSecondLevel())->rules('required');

        $form->text('url', '跳转链接');

        // 创建一个选择图片的框
        $form->image('image', '封面图片')->rules('required|image');

        $form->multipleImage('description_image', '详情图片')->removable()->sortable()->rules(function ($form) {
            // 仅在创建时应用 'required' 规则
            return $form->isCreating() ? 'required|image' : 'image';
        });

        // 创建一个富文本编辑器
        $form->quill('description', '商品描述')->rules('required');

        // 创建一组单选框
        $form->radio('on_sale', '上架')->options(['1' => '是', '0' => '否'])->default('0');

        $form->text('price', '价格')->rules('required|numeric');

        // 直接添加一对多的关联模型
        $form->hasMany('skus', 'SKU 列表', function (Form\NestedForm $form) {
            $form->text('title', 'SKU 名称')->rules('required');
            $form->text('description', 'SKU 描述')->rules('required');
            $form->text('price', '单价')->rules('required|numeric|min:0.01');
            $form->text('stock', '剩余库存')->rules('required|integer|min:0');
        });

        // 定义事件回调，当模型即将保存时会触发这个回调
        $form->saving(function (Form $form) {
            if ($form->input('on_sale') === 'on') {
                $form->on_sale = 1;
            } else if ($form->input('on_sale') === 'off') {
                $form->on_sale = 0;
            }
//            $form->model()->price = collect($form->input('skus'))->where(Form::REMOVE_FLAG_NAME, 0)->min('price') ?: 0;
        });

        return $form;
    }
}
